/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.constant.enums.DeleteFlagEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.shop.api.dto.GoodsMaterialDTO;
import com.pig4cloud.pigx.shop.api.entity.GoodsMaterial;
import com.pig4cloud.pigx.shop.service.GoodsMaterialService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * 素材库
 *
 * @author pig code generator
 * @date 2023-10-08 11:03:27
 */
@RestController
@AllArgsConstructor
@RequestMapping("/goods/material")
@Tag(name = "素材库管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GoodsMaterialController {

	private final GoodsMaterialService goodsMaterialService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param goodsMaterial 素材库
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getGoodsMaterialPage(Page page, GoodsMaterial goodsMaterial) {
		LambdaQueryWrapper<GoodsMaterial> lqw = new LambdaQueryWrapper<>();
		lqw.eq(GoodsMaterial::getMaterialGroupId, goodsMaterial.getMaterialGroupId());
		lqw.eq(GoodsMaterial::getDelFlag, DeleteFlagEnum.VALID.getType());
		lqw.orderByDesc(GoodsMaterial::getCreateTime);
		return R.ok(goodsMaterialService.page(page, lqw));
	}

	/**
	 * 通过id查询素材库
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(goodsMaterialService.getById(id));
	}

	/**
	 * 新增素材库
	 * @param goodsMaterial 素材库
	 * @return R
	 */
	@Operation(summary = "新增素材库", description = "新增素材库")
	@SysLog("新增素材库")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('goods_material_add')")
	public R save(@RequestBody GoodsMaterial goodsMaterial) {
		return R.ok(goodsMaterialService.add(goodsMaterial));
	}

	/**
	 * 修改素材库
	 * @param goodsMaterial 素材库
	 * @return R
	 */
	@Operation(summary = "修改素材库", description = "修改素材库")
	@SysLog("修改素材库")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('goods_material_edit')")
	public R updateById(@RequestBody GoodsMaterial goodsMaterial) {
		return R.ok(goodsMaterialService.update(goodsMaterial));
	}

	/**
	 * 通过id删除素材库
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除素材库", description = "通过id删除素材库")
	@SysLog("通过id删除素材库")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('goods_material_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(goodsMaterialService.removeById(id));
	}

	/**
	 * 素材移动分组
	 * @return
	 */
	@PostMapping("/moveMaterialGroup")
	@PreAuthorize("@pms.hasPermission('goods_material_edit')")
	public R moveMaterialGroup(@RequestBody GoodsMaterialDTO goodsMaterialDTO) {
		GoodsMaterial goodsMaterial = goodsMaterialService.getById(goodsMaterialDTO.getId());
		if (goodsMaterial != null) {
			// 删除原数据
			goodsMaterialService.removeById(goodsMaterial);

			// 添加新数据
			GoodsMaterial record = new GoodsMaterial();
			record.setMaterialGroupId(goodsMaterialDTO.getTargetMaterialGroupId());
			record.setMaterialName(goodsMaterial.getMaterialName());
			record.setMaterialUrl(goodsMaterial.getMaterialUrl());
			goodsMaterialService.save(record);
		}
		return R.ok();
	}

	/**
	 * 根据媒体库id列表获取图片路径
	 * @param goodsMaterialDTO
	 * @return
	 */
	@Inner
	@PostMapping("/convertImageIdToUrl")
	public R convertImageIdToUrl(@RequestBody GoodsMaterialDTO goodsMaterialDTO) {
		return R.ok(goodsMaterialService.convertImageIdToUrl(goodsMaterialDTO.getImageIds()));
	}

}
